# replicate.senate.R
#
# Compare AEH probability of Senate
# candidate choices to Todorov's actual.
#

cat('\nReplicating Senate scores ...\n')

comp <- read.csv("senate.csv",as.is=TRUE)

comp$comp <- comp$comp.legacy # For comparison to Todorov, use non-Z score version.
comp <- comp[is.na(comp$state)==F,]
comp <- comp[,c("state","year","comp","party")]     # Only relevant variables
# Reshape to wide (one row per election).
comp <- reshape(comp,v.names="comp",idvar=c("state","year"),
            timevar="party",direction="wide")
            
# Call in and configure Todorov data.
source("get.todorov.sendata.R")

# Merge them together
comp <- merge(comp,tod,all.x=T,all.y=F)

# Plot
comp$predicted.dem<-pnorm(comp$comp.dem-comp$comp.rep)

if(toEPS) {postscript("TableAndFiguresOutput/AppendixFigureB.eps",
                        width=6,height=6,horizontal=F)}
par(mar=c(5.1,4.1,2.1,2.1),font.sub=3,las=T)
plot(x=comp$COMP_D,y=comp$predicted.dem,
    main="Replication of Senate Results",
    xlab="Proportion Todorov et al. Respondents Picking Dem More Competent",
    ylab="Predicted Proportion Dem More Competent Given Our Scores",
    pch=ifelse(comp$year==2000,19,
        ifelse(comp$year==2002,15,17)),
    axes=F,cex=2,
    xlim=c(0,1),ylim=c(0,1))
axis(1);axis(2)
legend("topleft",pch=c(19,15,17),
    legend=c("2000","2002","2004"),bty='n')
abline(a=0,b=1,lty=2)       # 45 degree line
legend("right",bty='n',legend=paste('r =',round(cor(comp$COMP_D,comp$predicted.dem,
                                'pairwise.complete.obs'),2)) )
if(toEPS) { dev.off() }
